<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="新增机构" width="60%" destroyOnClose @ok="handleSubmit">
    <BasicForm @register="registerForm">

    </BasicForm>
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, useAttrs } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import {FormSchema} from '/@/components/Table';
  import { addOrg } from '../TSubmission.api';
  const emit = defineEmits(['register', 'success']);

  const formSchema: FormSchema[] = [
    {
      label: '机构名称',
      field: 'name',
      component: 'Input',
      dynamicRules: ({model,schema}) => {
        return [
          { required: true, message: '请输入机构名称!'},
        ];
      },
    },
    {
      label: '机构电话',
      field: 'phone',
      component: 'Input',
      dynamicRules: ({model,schema}) => {
        return [
          { required: true, message: '请输入机构电话!'},
        ];
      },
    },
    {
      label: '机构邮箱',
      field: 'email',
      component: 'Input',
      dynamicRules: ({model,schema}) => {
        return [
          { required: true, message: '请输入机构邮箱!'},
        ];
      },
    },
    // {
    //   label: '机构名称',
    //   field: 'realname',
    //   component: 'Input',
    //   dynamicRules: ({model,schema}) => {
    //     return [
    //       { required: true, message: '请输入机构名称!'},
    //     ];
    //   },
    // },
    {
      label: '统一信用代码',
      field: 'username',
      component: 'Input',
      dynamicRules: ({model,schema}) => {
        return [
          { required: true, message: '请输入统一信用代码!'},
        ];
      },
    },
  ]

  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    //labelWidth: 150,
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 24 }
  });

  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    console.log(data);
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false });
  });

  //表单提交事件
  async function handleSubmit(v: any) {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      await addOrg(values);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style>
  
</style>
